<template>
  <basic-container>
    <!--<h3>员工管理</h3>-->
    <avue-crud
      :option="option"
      :data="data"
      :page="page"
      @row-del="rowDel"
      v-model="form"
      :before-open="beforeOpen"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template slot-scope="scope" slot="menu">
        <el-button type="text" @click="open(scope.row)">初始化密码</el-button>
      </template>
      <!-- <template slot="state" slot-scope="{row}">
        <span>{{row.sexName}}</span>
      </template>-->
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getPeopleId,
  getPeopleList,
  remove,
  update,
  add,
  getNotice
} from "@/api/dept/user";
import { mapGetters } from "vuex";
import { getToken } from "@/util/auth";

export default {
  data() {
    return {
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        tip: false,
        addBtn: false,
        border: true,
        index: false,
        viewBtn: false,
        selection: false,
        columnBtn: false,
        refreshBtn: false,
        searchBtn: false,
        editBtn: false,
        delBtn: false,
        menu: true,
        column: [
          {
            label: "EID",
            prop: "id",
            row: true,
            search: false
          },
          {
            label: "创建时间",
            prop: "createTime",
            row: true,
            search: false
          },
          {
            label: "姓名",
            prop: "name",
            row: true,
            search: true
          },
          {
            label: "公司",
            prop: "tenantName",
            row: true,
            search: true
          },
          {
            label: "手机",
            prop: "phone",
            row: true,
            search: false
          },
          {
            label: "邮箱",
            prop: "email",
            row: true,
            search: false
          },
          {
            label: "手机/邮箱",
            prop: "number",
            row: true,
            search: true,
            hide: true,
            search: true
          }
          // {
          //   label: "操作",
          //   prop: "balance",
          //   row: true
          // }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.notice_add, false),
        viewBtn: this.vaildData(this.permission.notice_view, false),
        delBtn: this.vaildData(this.permission.notice_delete, false),
        editBtn: this.vaildData(this.permission.notice_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    open(item) {
      this.$confirm("此操作将重置密码, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          const param = {
            userIds: item.id
          };

          getPeopleId(param).then(res => {
            this.$message({
              type: "success",
              message: "重置密码成功！"
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "success",
            message: "已取消"
          });
        });
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      if (res.code != 200) {
        this.$message({
          message: res.msg,
          type: "error"
        });
      } else {
        this.$message({
          message: res.msg,
          type: "success"
        });
        this.onLoad(this.page);
      }
    },
    beforeAvatarUpload(file) {
      var testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
      const extension = testmsg === "xls";
      const extension2 = testmsg === "xlsx";
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!extension && !extension2) {
        this.$message({
          message: "上传文件只能是 xls、xlsx格式!",
          type: "error"
        });
      }
      if (!isLt2M) {
        this.$message({
          message: "上传文件大小不能超过 10MB!",
          type: "error"
        });
      }
      return extension || (extension2 && isLt2M);
    },
    rowSave(row, loading, done) {
      add(row).then(
        () => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        },
        error => {
          done();
          console.log(error);
        }
      );
    },
    rowUpdate(row, index, loading, done) {
      update(row).then(
        () => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        },
        error => {
          done();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    searchReset() {
      this.page.currentPage = 1;
      this.onLoad(this.page);
    },
    searchChange(params) {
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    handleImport() {},
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getNotice(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },

    onLoad(page, params = {}) {
      const param = {
        size: page.pageSize,
        current: page.currentPage,
        number: params.number || "",
        tenantName: params.tenantName,
        name: params.name
      };

      getPeopleList(param).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      });
    }
  }
};
</script>

<style>
</style>
